﻿Imports FinanceiroDAL
Public Class itensCtasReceberBLL
    Dim objDal As itensCtasReceberDAL
#Region "Propriedades"
    Property icrcodigo As Integer
    Property ctrcodigo As Integer
    Property pagcodigo As Integer
    Property icrvalor As Decimal ' a receber
    Property icrparcela As Integer 'num de parcelas
    Property icrdesconto As Decimal
    Property icrjam As Decimal
    Property icrvalrecebido As Decimal
    Property icrdtrecebimento As Nullable(Of Date)
    Property icrdtvencimento As Date
    Property concodigo As Integer
    Property stacodigo As Integer
    Property icrobs As String
    Property icrbolcheque As String
#End Region
#Region "Construtores"
    Sub New(ByVal codigo As Integer, ByVal tipo As Integer)
        carrega(codigo, tipo)
    End Sub
    Sub New()
    End Sub
    Sub New(ByVal icr_codigo As Integer, ByVal ctr_codigo As Integer, ByVal pag_codigo As Integer, icr_valor As Decimal, ByVal icr_parcela As Integer,
            ByVal icr_desconto As Decimal, ByVal icr_jam As Decimal, ByVal icr_valrecebido As Decimal, ByVal dt_recebimento As Nullable(Of Date),
            ByVal icr_dtvencimento As Date, ByVal con_codigo As Integer, ByVal sta_codigo As Integer, ByVal icr_obs As String, ByVal icr_bolcheque As String)
        Me.icrcodigo = icr_codigo
        Me.ctrcodigo = ctr_codigo
        Me.pagcodigo = pag_codigo
        Me.icrvalor = icr_valor
        Me.icrparcela = icr_parcela
        Me.icrdesconto = icr_desconto
        Me.icrjam = icr_jam
        Me.icrvalrecebido = icr_valrecebido
        Me.icrdtrecebimento = dt_recebimento
        Me.icrdtvencimento = icr_dtvencimento
        Me.concodigo = con_codigo
        Me.stacodigo = sta_codigo
        Me.icrobs = icr_obs
        Me.icrbolcheque = icr_bolcheque
    End Sub
#End Region
#Region "Construtores"
    Sub carrega(ByVal codigo As Integer, ByVal tip As Integer)
        Dim tab As DataTable
        Try
            objDal = New itensCtasReceberDAL
            tab = objDal.localizar(codigo, tip)   'modificar em DAL
            If (tab.Rows.Count > 0) Then
                Me.icrcodigo = tab.Rows(0).Item("icr_codigo").ToString
                Me.ctrcodigo = tab.Rows(0).Item("ctr_codigo").ToString
                Me.pagcodigo = tab.Rows(0).Item("pag_codigo").ToString
                Me.icrvalor = Decimal.Parse(tab.Rows(0).Item("icr_valor").ToString)
                Me.icrparcela = tab.Rows(0).Item("icr_parcela").ToString
                Me.icrdesconto = tab.Rows(0).Item("icr_desconto").ToString
                Me.icrjam = tab.Rows(0).Item("icr_jam").ToString
                Me.icrvalrecebido = tab.Rows(0).Item("icr_valrecebido").ToString
                If (tab.Rows(0)("icr_dtrecebimento") Is DBNull.Value) Then
                    Me.icrdtrecebimento = Nothing
                Else
                    Me.icrdtrecebimento = tab.Rows(0).Item("icr_dtrecebimento").ToString
                End If
                Me.icrdtvencimento = tab.Rows(0).Item("icr_dtvencimento").ToString
                Me.concodigo = tab.Rows(0).Item("con_codigo").ToString
                Me.stacodigo = tab.Rows(0).Item("sta_codigo").ToString
                Me.icrobs = tab.Rows(0).Item("icr_obs").ToString
                Me.icrbolcheque = tab.Rows(0).Item("icr_bolcheque").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDal = Nothing
        End Try
    End Sub
#End Region
#Region "Limpar"
    Sub limpar()
        Me.icrcodigo = Nothing
        Me.ctrcodigo = Nothing
        Me.pagcodigo = Nothing
        Me.icrvalor = Nothing
        Me.icrparcela = Nothing
        Me.icrdesconto = Nothing
        Me.icrjam = Nothing
        Me.icrvalrecebido = Nothing
        Me.icrdtrecebimento = Nothing
        Me.icrdtvencimento = Nothing
        Me.concodigo = Nothing
        Me.stacodigo = Nothing
        Me.icrobs = Nothing
        Me.icrbolcheque = Nothing

    End Sub
#End Region
#Region "Inserir"
    Function inserealteraICtasRec() As Boolean
        Try
            objDal = New itensCtasReceberDAL
            If (Me.valida) Then 'em cr true
                If Me.stacodigo = 0 Then 'ret 6
                    Me.stacodigo = 4
                End If
                objDal.inserealteraICtasRec(Me.icrcodigo, Me.ctrcodigo, Me.icrdtrecebimento, Me.icrvalor, Me.icrobs, Me.pagcodigo, Me.icrvalrecebido, Me.icrdesconto, Me.icrjam, Me.concodigo, Me.stacodigo, Me.icrparcela, Me.icrdtvencimento, Me.icrbolcheque)
                Return True
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDal = Nothing
        End Try
        Return False
    End Function
#End Region
#Region "Excluir"
    Function excluir(ByVal cod As Integer) As Boolean
        Try
            objDal = New itensCtasReceberDAL
            objDal.excluir(cod)
            Return True 'retornou true em cr
        Catch ex As Exception
            Throw ex
            Return False
        End Try
    End Function
#End Region
#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        If (Me.icrvalor <= 0) Then Throw New Exception("Preencha o valor da parcela")
        If (Me.icrparcela = 0) Then Throw New Exception("Verifique o numero da parcela")
        'If (Me.stacodigo <> 4) Then Throw New Exception("Status Inválido!")
        '  If (Me.stacodigo <> 6) Then Throw New Exception("Status Inválido!")
        '  If (Me.icrdtvencimento < Date.Now) Then Throw New Exception("Data de vencimento menor que a data atual")
        Return True
    End Function
#End Region
#Region "Soma Parcelas" '
    Function Somaparcelas(ByVal cod As Integer) As Decimal
        Dim ret As Decimal
        Try
            objDal = New itensCtasReceberDAL
            ret = objDal.Somaparcelas(cod)
        Catch ex As Exception
            Throw ex
        Finally
            objDal = Nothing
        End Try
        Return ret
    End Function
#End Region
End Class
